﻿//-------------------------------------------------------------------------------------------------
// <copyright file="Ensure.cs" company="Oliver Hallam">
// Copyright (c) 2008 Oliver Hallam
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// </copyright>
//-------------------------------------------------------------------------------------------------

namespace DBLite.Contracts
{
    using System;
    using System.Diagnostics.Contracts;
    
    /// <summary>
    /// Provides methods for runtime contract verification, throwing appropriate exceptions when contracts have been
    /// broken.
    /// </summary>
    /// <remarks>
    /// These methods should be used to enforce contracts for all public facing methods.
    /// </remarks>
    internal static class Ensure
    {
        #region Static Methods

        /// <summary>
        /// Ensures that an argument is within particular bounds, and raises an
        /// <see cref="ArgumentOutOfRangeException"> if not.</see>
        /// </summary>
        /// <param name="argument">The argument to test.</param>
        /// <param name="count">The count of the collection <paramref name="argument"/> is indexing.</param>
        /// <param name="argumentName">The name of the argument.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="argument"/> was out of range.</exception>
        ///// <seealso cref="Assert.ArgumentInRange"/>
        public static void ArgumentInRange(int argument, int count, string argumentName)
        {
            Contract.Requires(argumentName != null);
            Contract.Ensures(argument >= 0);
            Contract.Ensures(argument < count);
            Contract.EnsuresOnThrow<ArgumentOutOfRangeException>(argument < 0 || argument > count);

            if (argument < 0 || argument >= count)
            {
                throw new ArgumentOutOfRangeException(argumentName, ExceptionResources.ArgumentOutOfRange(argumentName));
            }
        }

        /// <summary>
        /// Ensures that an argument is not  <see langword="null"/>, and raises an
        /// <see cref="ArgumentNullException"/> if it is.
        /// </summary>
        /// <param name="argument">The argument to test.</param>
        /// <param name="argumentName">The name of the argument.</param>
        /// <exception cref="ArgumentNullException"><paramref name="argument"/> is
        /// <see langword="null"/></exception>
        public static void ArgumentNotNull(
            object argument,
            string argumentName)
        {
            Contract.Requires(argumentName != null);
            Contract.Ensures(argument != null);
            Contract.EnsuresOnThrow<ArgumentNullException>(argument == null);

            if (argument == null)
            {
                throw new ArgumentNullException(argumentName, ExceptionResources.ArgumentNull(argumentName));
            }
        }

        #endregion
    }
}